package com.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.Constants;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.*;
import com.example.exception.CustomException;
import com.example.mapper.AdminMapper;
import com.example.mapper.RepairMapper;
import com.example.mapper.WorkerMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 管理员业务处理
 **/
@Service
public class WorkerService {

    @Resource
    private WorkerMapper workerMapper;
    @Autowired
    private RepairMapper repairMapper;

    /**
     * 新增
     */
    public void add(Worker worker) {
        Worker dbAdmin = workerMapper.selectByUsername(worker.getUsername());
        if (ObjectUtil.isNotNull(dbAdmin)) {
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        if (ObjectUtil.isEmpty(worker.getPassword())) {
            worker.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        if (ObjectUtil.isEmpty(worker.getName())) {
            worker.setName(worker.getUsername());
        }
        worker.setRole(RoleEnum.WORKER.name());
        workerMapper.insert(worker);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        workerMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            workerMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Worker worker) {
        workerMapper.updateById(worker);
    }

    /**
     * 根据ID查询
     */
    public Worker selectById(Integer id) {
        return workerMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Worker> selectAll(Worker worker) {
        return workerMapper.selectAll(worker);
    }

    /**
     * 分页查询
     */
    public PageInfo<Worker> selectPage(Worker worker, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Worker> list = workerMapper.selectAll(worker);
        return PageInfo.of(list);
    }

    /**
     * 登录
     */
    public Account login(Account account) {
        Account dbAdmin = workerMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbAdmin)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!account.getPassword().equals(dbAdmin.getPassword())) {
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        // 生成token
        String tokenData = dbAdmin.getId() + "-" + RoleEnum.WORKER.name();
        String token = TokenUtils.createToken(tokenData, dbAdmin.getPassword());
        dbAdmin.setToken(token);
        return dbAdmin;
    }

    /**
     * 注册
     */
    public void register(Account account, String phone) {
        Worker worker = new Worker();
        BeanUtils.copyProperties(account, worker);
        worker.setPhone(phone);
        worker.setAvatar(account.getAvatar());
        add(worker);
    }


    public List<Worker> selectAllAssign(Worker worker) {
        List<Worker> workers = workerMapper.selectAll(new Worker());
        Repair r = new Repair();
        r.setRepairStatus("处理中");
        List<Repair> repairs = repairMapper.selectAll(r);
        for(Worker w : workers) {
            w.setRepairCount(0);
            for(Repair repair : repairs) {
                if(w.getId().equals(repair.getWorker1Id())) {
                    w.setRepairCount(w.getRepairCount() + 1);
                }
                if(w.getId().equals(repair.getWorker2Id())) {
                    w.setRepairCount(w.getRepairCount() + 1);
                }
            }
        }
        return workers;
    }
}